require(["config"], function() {
	require(["jquery", "template", "header", "cookie"], function($, template) {
		function Cart(){
			this.products = null;
			this.load();
			
			this.addListener();
		}
		
		Cart.prototype = {
			constructor:Cart,
			load:function(){
				$.cookie.json = true;
				var products = this.products = $.cookie("cart") || [];
				//判断是否有购物车商品
				if(products.length === 0){
					$(".empty").removeClass("hidden").siblings(".not-empty").hide();
					return;
				}
				//购物车非空
				$(".empty").hide().siblings(".not-empty").removeClass("hidden");
				//渲染模板
				var data = {products:products};
				console.log(data);
				var html = template("cart-template",data);
				$(".t_container_wrap").html(html);
				this.calcTotal();
			},
			//添加事件监听
			addListener: function(){
				//删除
				$(".del").click($.proxy(this.delHandler,this));
				//+-数量
				$(".change-amount").on("click",".decrement,.increment",$.proxy(this.decIncHandler,this))
				//输入数量处理
				$(".change-amount").on("blur","input",$.proxy(this.inputHandler,this));
				//全选
				$(".ck_all").on("click",$.proxy(this.ckAllHandler,this));
				// 部分选中
				$(".check-all").on("click", $.proxy(this.ckProdHandler, this));
			},
			//删除
			delHandler:function(event){
				var src = event.target;
				var _id = $(src).parents("ul").siblings(".prod_id");
				var id = _id.text();
				this.products = this.products.filter(function(prod){
					if(prod.id === id)
						return false;
					return true;
				});
//				console.log(this.products)删除cookie数据
				$.cookie("cart",this.products,{expirse:10,path:"/"});
				//删除dom树中节点
				$(src).parents("ul").parents(".middle").parents(".context").parents(".t_container").remove();
				if(this.products.length === 0){
					$(".empty").removeClass("hidden").siblings(".not-empty").hide();
				}
				this.calcTotal();
			},
			
			//加减数量
			decIncHandler: function(event){
				var src = event.target;
//				console.log(src)
				var _id = $(src).parents("li").parents("ul").siblings(".prod_id").text();				
				var products = this.products.filter(function(prod){
					return prod.id == _id;
				})[0];
				if($(src).is(".decrement")){
					if(products.amount <=1)
						return
					products.amount--;
				}else{
					products.amount++;
				}
				$.cookie("cart",this.products,{expirse:10,path:"/"});
				$(src).siblings("input").val(products.amount);
				var countString = "￥"+products.amount*products.price;
				$(src).parents("li").siblings(".prod-count").text(countString);
				$(src).parents("li").parents("ul").parents(".middle").siblings(".bottom")
				.children(".product-all-price").text("商品金额合计：￥"+products.price*products.amount);
				this.calcTotal();
			},
			
			//输入数量处理
			inputHandler:function(event){
				var src = event.target;
				var _id = $(src).parents("li").parents("ul").siblings(".prod_id").text();
				var products = this.products.filter(prod => prod.id===_id)[0];				
				var _amount = $(src).val();				
				//正则判断输入数据符合规范
				var reg = /^[1-9]\d*$/;
				if(!reg.test(_amount)){//不合格
					console.log(products.amount)
					$(src).val(products.amount);
					return;
				}
				products.amount = $(src).val();
				$.cookie("cart",this.products,{expires:10,path:"/"});
				$(src).parents("li").siblings(".prod-count").text("￥"+products.amount*products.price);
				$(src).parents("li").parents("ul").parents(".middle").siblings(".bottom")
				.children(".product-all-price").text("商品金额合计：￥"+products.price*products.amount);
				this.calcTotal();
			},
			//全选
			ckAllHandler:function(event){
				var src = event.target;
				var status = $(src).prop("checked");
//				console.log(status);
				$(".check-all").prop("checked",status);//商品勾选
				$(".ck_all").prop("checked",status);
				this.calcTotal();
			},
			//部分选中
			ckProdHandler: function(event){
				var src = event.target;
				//获取选中框的数量
				var count = $(".check-all:checked").length;
//				console.log(count)
				var status = count == this.products.length;
				
				$(".ck_all").prop("checked",status);	
				this.calcTotal();
			},
			//计算
			calcTotal:function(){
				// 将选中行中的小计金额累加
				var sum = 0;
				// 选中行中的复选框
				$(".check-all:checked").each(function(index, element) {
					// console.log(index, element, this, element == this);
					sum += Number($(this).parents("li").siblings(".prod-count").text().slice(1));
				});
				// 显示底部合计金额		
				$(".integer").text(parseInt(sum)+".");
				sum = sum.toString();
				$(".decimal").text((sum.split("."))[1]);
				
				//显示选购商品数量
				$(".total-quantity").text($(".check-all:checked").length);
				$(".total-product-price").text(sum);
				
			}
		}
		
		new Cart();
	});
});